<template>
  <div class="mgtPassword">
    <x-header v-show="!isWeChar">
      <div slot="default" class="common__header--title">{{pageTitle}}</div>
      <div @click="goBack" slot="overwrite-left"><img slot="icon" src="~assets/x3/ico/goback1.png" class="go-back"></div>
    </x-header>
    <div class="mgtPassword_inputDiv">
      <x-input type="tel" v-model="mobileLabel" :max="StrLenth.mobile" text-align="right" readonly>
        <span slot="label">手机号码</span>
      </x-input>
      <x-input type="password" v-model="password" :min="StrLenth.pwdMin" :max="StrLenth.pwdMax" placeholder="请输入新密码">
        <img slot="label" style="padding-right:10px;display:block;" src="~assets/x3/ico/password_lock.png" width="18">
      </x-input>
      <x-input v-if="haveImgCode" v-model="imgCode" :max="StrLenth.img" placeholder="请输入图形验证码">
        <img slot="label" style="padding-right:10px;padding-top:5px;display:block;" src="~assets/x3/ico/ico-input-img_code.png" width="18">
        <span slot="right" class="img__code--change" @click="changeImgCode()">换一张</span>
        <img slot="right" class="img__code" :src="imgCodeSrc" @click="changeImgCode()">
      </x-input>
      <x-input type="tel" :max="StrLenth.sms" v-model="mobileCode" placeholder="请输入短信验证码">
        <img slot="label" style="padding-right:10px;padding-top:5px;display:block;" src="~assets/x3/ico/ico-input-Mcode.png" width="18">
        <x-button action-type="button" slot="right" mini class="linkMobileCode" @click.native="sendSmsCode" :text="sendBtnText" :disabled="isSendOk"></x-button>
      </x-input>
    </div>
    <div class="mgtPassword_errorMsg" v-show="haveError">
      <badge text="!"></badge> {{errorMessage}}
    </div>
    <x-button text="保存" @click.native="processForm" type="primary" class="mgtPassword_process_btn"></x-button>
  </div>
</template>
<script>
import { XHeader, XInput, XButton, Badge, Alert, AlertPlugin, Confirm } from "vux";
import { RouteUrl, Mutations, StrLenth } from "src/common/const";
import Validation from "src/common/util/validation";
import { RequestUrl } from "src/common/url";
import { mapState } from "vuex";

export default {
  components: { XHeader, XInput, XButton, Badge, Alert, AlertPlugin, Confirm },
  computed: mapState(["user", "isWeChar"]),
  data() {
    return {
      StrLenth: {}, //字符长度常量
      pageTitle: "修改登录密码",
      mobile: "", //手机号
      mobileLabel: "", //展示的手机号
      password: "", //新密码
      mobileCode: "", //验证码
      isSendOk: false, //发送按钮是否禁用
      sendBtnText: "获取验证码", //发送按钮文本内容
      haveError: false, //是否显示错误
      errorMessage: "", //错误信息

      imgCodeSrc: RequestUrl.GET_VALIDATE_CODE, //验证码图片路径
      imgCode: "", //图形码
      haveImgCode: true //是否需要图形码,false=因为默认为密码登录因此需要,动态码登录第一次不需要
    };
  },
  methods: {
    //返回
    goBack() {
      this.$common.goBackUrl(this, RouteUrl.ACCOUNT);
    },
    changeImgCode() {
      this.imgCodeSrc = RequestUrl.GET_VALIDATE_CODE + "?a=" + Math.random();
    },
    // 发送验证码
    sendSmsCode() {
      /**
       * 发送验证码,返回data.msg=信息,data.state=0表示成功
       * isSendOk,true=禁用(disabled=true)
       * sendBtnText,验证码按钮文本显示内容
       */
      let imgcode = null;
      if (this.haveImgCode) {
        //需要图形码时传递参数
        imgcode = this.imgCode;
        //校验-图形码
        let msg = Validation.isImgCode(imgcode);
        if (msg != null) {
          this.errorMessage = msg;
          this.haveError = true;
          return;
        }
      }
      this.$common.sendSMS(this, this.mobile, imgcode, this.isSendOk, this.pageTitle);
    },
    //提交
    processForm() {
      let userDto = this.user.userDto;
      //默认请求无验证码接口
      let postUrl = RequestUrl.UPDATE_PWD_BY_MOBILE;
      let initData = {
        TYPE: "FORM",
        id: userDto.userCode,
        mobile: this.mobile,
        password: this.password,
        mobileCode: this.mobileCode
      };
      //校验-手机号
      let msg = Validation.isPhone11(initData.mobile);
      if (msg != null) {
        this.errorMessage = msg;
        this.haveError = true;
        return;
      }
      //校验-密码
      msg = Validation.isPassword(initData.password);
      if (msg != null) {
        this.errorMessage = msg;
        this.haveError = true;
        return;
      }
      if (this.haveImgCode) {
        //图形码
        postUrl = RequestUrl.UPDATE_PWD_BY_MOBILE_IMG;
        initData.imgCode = this.imgCode;
        //校验-图形码
        msg = Validation.isImgCode(initData.imgCode);
        if (msg != null) {
          this.errorMessage = msg;
          this.haveError = true;
          return;
        }
      }
      //校验-手机验证码
      msg = Validation.isSmsCode(initData.mobileCode);
      if (msg != null) {
        this.errorMessage = msg;
        this.haveError = true;
        return;
      }
      this.haveError = false;
      this.errorMessage = "";
      let _this = this;
      _this.$common.storeCommit(_this, Mutations.UPDATE_LOADING_STATUS, true);
      //提交密码操作
      _this.$http.post(postUrl, initData).then(function (res) {
        _this.$common.storeCommit(_this, Mutations.UPDATE_LOADING_STATUS, false);
        if (res.state != 0) {
          if (res.result == "图形码过期" || res.result == "图形码错误") {
            _this.changeImgCode();//更换图形验证码
          }
          _this.errorMessage = res.result;
          _this.haveError = true;
          // _this.haveImgCode = true; //第一次未启用时，启用图形验证码
          if (_this.errorMessage.indexOf("手机号不匹配") >= 0 ||
            _this.errorMessage.indexOf("密码相同") >= 0) {
            _this.isSendOk = false; //启用发送按钮和手机号输入
            _this.sendBtnText = "获取验证码";
            //清除验证码倒计时
            _this.$common.clearTimeOutForSms();
            return;
          }
          return;
        }
        //清除USER_INFO信息
        _this.$common.storeCommit(_this, Mutations.REMOVE_USER_INFO);

        //判断是否有抽奖信息
        _this.$common.storeCommit(_this, Mutations.IS_SHOW_RED, _this);


        //3s后自动或关闭窗口跳转到登录
        let showTime = setTimeout(() => {
          _this.$vux.alert.hide();
          _this.$common.goUrl(_this, RouteUrl.LOGIN);
        }, 3000);
        //提示信息
        _this.$vux.alert.show({
          content: _this.$common.alertMsg("修改成功", "green;"),
          buttonText: "重新登录",
          onHide() {
            clearTimeout(showTime);
            _this.$common.goUrl(_this, RouteUrl.LOGIN);
          }
        });
      });
    },
    //初始化当前页面信息
    initPage() {
      //清除验证码倒计时
      this.$common.clearTimeOutForSms();
      if (this.user.isLogin) {
        //手机号
        this.mobile = this.user.userDto.mobile;
        //展示的手机号
        this.mobileLabel = this.mobile.substring(0, 3) + "*****" + this.mobile.substring(this.mobile.length - 3, this.mobile.length);
      } else {
        this.$common.goUrl(this, RouteUrl.LOGIN);
      }
    }
  },
  mounted() {
    document.title = this.pageTitle;
    this.StrLenth = StrLenth; //字符长度常量
    this.$init.subPageInit(this);
    //初始化当前页面信息
    this.initPage();
  }
};
</script>
<style lang="scss" scoped>
//input区域
.mgtPassword_inputDiv {
  margin: 15px auto;
  width: 95%;
  height: auto;
  border-radius: 5px;
  background-color: white;
  span {
    font-size: 14px;
  }
  .weui-cell {
    border-top: 1px solid #efefef;
  }
  .weui-cell:before {
    border-top: 0px;
  }
}

//图形码
.img__code {
  width: 80px;
  height: 31px;
  margin-left: 3px;
  vertical-align: middle;
}

//换一张图形码
.img__code--change {
  margin-left: 3px;
  vertical-align: middle;
  font-size: 14px;
}

//获取验证码按钮
.linkMobileCode {
  padding: 0px;
  width: 80px;
  height: 28px;
  line-height: 28px;
  font-size: 12px;
  color: #C8161D;
  border: 1px solid #C8161D;
  background-color: white;
}

//提交按钮
.mgtPassword_process_btn {
  width: 95%;
  font-size: 20px;
}

//错误信息
.mgtPassword_errorMsg {
  color: red;
  font-size: 14px;
  margin-left: 10px;
  height: 36px;
}

//弹窗信息
.weui-dialog__bd {
  height: 30px;
  line-height: 30px;
}

//修改默认按钮失效时背景颜色
.weui-btn_disabled.weui-btn_primary {
  background-color: #cfcfcf;
}
</style>
